package com.zhisheng.orders.controller;


import com.alipay.api.AlipayApiException;
import com.zhisheng.dto.order.OrderDto;
import com.zhisheng.orders.service.OrdersService;
import com.zhisheng.result.MyResponseResult;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * (Orders)表控制层
 *
 * @author 全哥
 * @since 2025-02-24 10:35:47
 */
@RestController
@RequestMapping("orders")
public class OrdersController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersService ordersService;



    @GetMapping("createOrderId")
    public MyResponseResult<Long> createOrderId() {
        return MyResponseResult.success(ordersService.createOrderId());
    }


    @PostMapping("createOrderAndPay")
    public void createOrderAndPay(@RequestBody OrderDto orderDto,HttpServletResponse response) throws Exception {
        String s = ordersService.createOrderAndPay(orderDto);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println(s);
    }

    @GetMapping("toAliPay/{oid}")
    public void toAliPay(@PathVariable("oid") Long oid, HttpServletResponse response) throws Exception {
        String res = ordersService.toAliPay(oid);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println(res);
    }

    @RequestMapping("callback")
    public MyResponseResult<String> callback(@RequestParam Map<String, String> paramsMap) throws AlipayApiException {
        String msg = ordersService.callback(paramsMap);
        System.out.println("支付宝回调了,回调的参数是:-----------------------------------------------------------------------------------------------------------------------------------"+paramsMap);
        return MyResponseResult.success(msg);
    }


}

